/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.IOException;
import java.util.Timer;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * tampilan splash screen
 */
public class Splash extends Canvas {

    Image iSplash;
    int delay = 3000;
    Timer iTimer;
    String text = "Press any key to continue";
    SplashTimerTask iTimerTask;

    public Splash() {
        setFullScreenMode(true);
        try {
            iSplash = Image.createImage("/splash.png");     //ganti splash
            // sesuaikan ukuran gambar dengan melakukan resize
            if (getHeight() < iSplash.getHeight()) {
                int rasio = (getHeight() * 10) / iSplash.getHeight();
                String strRasio = "0." + rasio;
                float factor = Float.parseFloat(strRasio);
                if (factor != 0) {
                    iSplash = Resizer.resizeImage(iSplash, factor, Resizer.MODE_BOX_FILTER);
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * inisialisasi timer dan timer task serta waktu untuk menampilkan timer task
     */
    void startTimer() {
        iTimer = new Timer();
        iTimerTask = new SplashTimerTask();
        iTimer.schedule(iTimerTask, 10000);
    }

    /**
     * menghentikan timer
     */
    void stopTimer() {
        if (iTimer != null) {
            try {
                iTimerTask.cancel();
            } catch (Exception e) {
            }
            try {
                iTimer.cancel();
            } catch (Exception e) {
            }
            iTimer = null;
            iTimerTask = null;
        }
    }

    protected void keyPressed(int keyCode) {
        KamusMidlet.midlet.splashTimeOut();
    }

    protected void paint(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());
        if (iSplash != null) {
            //draw splash screen
            int x = getWidth() >> 1;
            int y = getHeight() >> 1;
            g.drawImage(iSplash, x, y, Graphics.HCENTER | Graphics.VCENTER);

            //draw text
            g.setFont(Font.getDefaultFont());
            y = getHeight() - g.getFont().getBaselinePosition();
            g.setColor(0xffffff);
            g.drawString(text, x, y, Graphics.HCENTER | Graphics.BOTTOM);
            g.setColor(0x000000);
            g.drawString(text, x + 1, y + 1, Graphics.HCENTER | Graphics.BOTTOM);
        }
    }
}
